package com.xyj;

/**
 * @author youjia xu
 * data:2023/11/2 6:38
 * @since
 **/
class Fruit{}
class Apple extends Fruit{}
class Jonathan extends Apple{}
class Orange extends Fruit{}


public class CovariantArray {
    public static void main(String[] args) {
        // 向上转型，编译可以通过，但是运行时报异常
        Fruit[] fruits = new Apple[10];
        fruits[0] = new Apple();
        fruits[1] = new Jonathan();
        //java.lang.ArrayStoreException: com.xyj.Fruit
        try {
            fruits[0] = new Fruit();
        }catch (Exception e){
            System.out.println(e);
        }
        //java.lang.ArrayStoreException: com.xyj.Orange
        try {
            fruits[0] = new Orange();
        }catch (Exception e){
            System.out.println(e);
        }



    }

}
